package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.cdc.sys.entity.DataTableEntity;
import com.dhcc.cdc.sys.service.ICdcDataService;
import com.dhcc.cdc.sys.service.IDataTableService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 院区数据、科室数据、患者数据、检验数据等
 * 
 * @ClassName: CdcDataController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-31 17:43:57
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcdata")
public class CdcDataController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcdata/";

	@Autowired
	private ICdcDataService cdcDataService;
    @Autowired
    private IDataTableService dataTableService;
	

    /**
     * 跳转到院区数据、科室数据、患者数据、检验数据等列表首页
     */
    @RequestMapping("")
    @Permission("cdcdata:list")
    public String index() {
        return PREFIX + "cdcdata.html";
    }
	
    /**
     * 跳转到新增院区数据、科室数据、患者数据、检验数据等
     */
    @RequestMapping("/to_add")
    @Permission("cdcdata:add")
    public String toAdd() {
        return PREFIX + "cdcdata_add.html";
    }
	
	/**
     * 跳转到修改院区数据、科室数据、患者数据、检验数据等
     */
    @RequestMapping("/to_update/{cdcDataId}")
    @Permission("cdcdata:update")
    public String toUpdate(@PathVariable Long cdcDataId, Model model) {
        CdcDataEntity cdcData = this.cdcDataService.selectById(cdcDataId);
        model.addAttribute("cdcData",cdcData);
        return PREFIX + "cdcdata_edit.html";
    }

    /**
     * 获取院区数据、科室数据、患者数据、检验数据等列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcdata:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcDataEntity> page = new PageFactory<CdcDataEntity>().defaultPage();
        List<CdcDataEntity> result = this.cdcDataService.page(page,params);
        page.setRecords((List<CdcDataEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增院区数据、科室数据、患者数据、检验数据等
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcdata:add")
    @BussinessLog(value = "新增院区数据、科室数据、患者数据、检验数据等")
    public Object add(CdcDataEntity cdcData) {
        if (CommonUtil.isOneEmpty(cdcData)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcData.insert();
        return R.ok();
    }

    /**
     * 删除院区数据、科室数据、患者数据、检验数据等
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcdata:delete")
    @BussinessLog(value = "删除院区数据、科室数据、患者数据、检验数据等")
    public Object delete(@RequestParam Long cdcDataId) {
	
        this.cdcDataService.deleteById(cdcDataId);

        return R.ok();
    }

    /**
     * 修改院区数据、科室数据、患者数据、检验数据等
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcdata:update")
    @BussinessLog(value = "业务数据类型")
    public Object update(CdcDataEntity cdcData) {
        if (CommonUtil.isOneEmpty(cdcData)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcDataEntity old = this.cdcDataService.selectById(cdcData.getId());

        old.setCode(cdcData.getCode());
        old.setDescription(cdcData.getDescription());
        old.setNote(cdcData.getNote());
        old.setIsActive(cdcData.getIsActive());
        old.setOrganizationId(cdcData.getOrganizationId());
        old.setCdcDataTypeId(cdcData.getCdcDataTypeId());
        old.updateAllColumnById();
        return R.ok();
    }
    
	@RequestMapping(value = "/query")
    @Permission("cdcdata:list")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params) {
        List<CdcDataEntity> result = this.cdcDataService.list(params);
    	return result;
    }
    
    @RequestMapping(value = "/tree/{dataDesc}/{dataTypeCode}")
    @ResponseBody
    public List<ZTreeNode> tree(@PathVariable String dataDesc,@PathVariable(required = false) String dataTypeCode) {
        if ("null".equals(dataDesc)) {
            dataDesc=null;
        }
        if ("null".equals(dataTypeCode)) {
            dataTypeCode=null;
        }
    	return this.cdcDataService.tree(dataDesc,dataTypeCode);
    }

    @RequestMapping(value = "/to_cttable/{cdcDataId}")
    @Permission("cdcdata:add")
    public String toSaveCTTable(@PathVariable Long cdcDataId, Model model) {
        model.addAttribute("cdcDataId",cdcDataId);
        return PREFIX + "cdcdata_table.html";
    }

    @RequestMapping(value = "/savecttable")
    @Permission("cdcdata:add")
    @ResponseBody
    public Object SaveCTTable(@RequestParam String tableIds,@RequestParam Long cdcDataId) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("sys_data_id", cdcDataId);
        this.dataTableService.deleteByMap(map);

        String[] tableId = tableIds.split(",");
        for (String id : tableId) {
            Long idL = Long.parseLong(id);
            DataTableEntity dataTable = new DataTableEntity();
            dataTable.setSysDataId(cdcDataId);
            dataTable.setSysDictionaryId(idL);
            dataTable.insert();
        } 
        return R.ok();
    }

    @RequestMapping(value = "/treeAll/{dataDesc}/{dataTypeCode}")
    @ResponseBody
    public List<ZTreeNode> treeAll(@PathVariable String dataDesc,@PathVariable(required = false) String dataTypeCode) {
        if ("null".equals(dataDesc)) {
            dataDesc=null;
        }
        if ("null".equals(dataTypeCode)) {
            dataTypeCode=null;
        }
        return this.cdcDataService.treeAll(dataDesc,dataTypeCode);
    }
}
